package com.example.ten.article;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.ten.R;
import com.example.ten.Utils.ContentUtils;
import com.example.ten.base.BaseFragment;
import com.example.ten.image.ImageFragmentPagerAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * 文章界面
 */
public class ArticleFragment extends BaseFragment {

    ViewPager articleVp;
    List<Fragment>fragmentList;
    private ImageFragmentPagerAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        fragmentList=new ArrayList<>();//存放页面的数据源
        View view=inflater.inflate(R.layout.fragment_style, container, false);
        articleVp=view.findViewById(R.id.frag_vp);
        //获取数据
        loadData(getContext(), ContentUtils.ARTICLE_URL,
                ContentUtils.FILE_ARTICLE,false,true,false);
        return view;
    }

    @Override
    public void showDataFromNet(String s) {
        //解析
        ArticleBean bean = new Gson().fromJson(s, ArticleBean.class);
        List<ArticleBean.ResultBean> resultList = bean.getResult();
        for (int i = 0; i < resultList.size(); i++) {
            ArticleBean.ResultBean resultBean = resultList.get(i);
            Fragment articleFragment=new ItemArticleFragment();
            Bundle bundle = new Bundle();
            bundle.putSerializable("article",resultBean);
            articleFragment.setArguments(bundle);
            //把fragment放入聚合
            fragmentList.add(articleFragment);
        }
        adapter = new ImageFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
        articleVp.setAdapter(adapter);
    }

}
